Added wrap-around logic for the top and bottom of the screen.