use CM's event handling
jshinting and small code cleanup
more code cleanup