cleanup and fix caret positioning