better implementation which keeps user's cursor position