Refactor improvements to caret.js