I can't take any credit as it's not originally my code. It is a port over from action script. Not a perfect copy since the author unnecessarily used memory for additional variables when he could have replaced the values of the two needed. That and error codes.