Coffeescript rewritten to Javascript and some minor changes