Improved cursor code