Refactoring the controllers logic