Refactor main.js and rewrite portions to coffeescript for legibility