Tested and fixed horizontal/vertical movement function