working caret position function