Cleaned up and converted to CoffeeScript