Properly set caret position