Since you are working off of global variables for your input, you should be able to single step through your code using the Do It facility.
Beware of using the '=' test for end of game detection.
You want to allow for overshoot, so use '>' instead.
Otherwise, your test is paper thin.