converted code to coffeescript, refactoring