Fixed cursor position returning an index out of range