Refactor caret.js
Improve caret.js