issue #591 - prevent cursor from going off the edge