Fix logic for end > cursor_pos