Fix cursor index out of bounds