better handling of caret and scroll position