Moved a lot of the logic from the controller to the model