Some refactoring in caret.js