Refactor to state pattern instead of generators and use CoffeeScript