Refactor caret.js