refactored more and updated for new CodeMirror