Refactored some code in the main controller