Fix cursor edge cases and test