Refactor javascript code and convert to coffeescript