Added stackoverflow answer for caret positioning