Convert to coffee script, change some logic around