Fixed bug where prev_x was used for both left and right movement.
Fixed bug where prev_y was used for both up and down movement.
Fixed bug where prev_x/y was stored independent of direction.
Fixed bug where moving right was sometimes considered possible, when it was not.
Keep the recursion levels sane.