Move some code from the controller to the model