refactors some code in the controller