Migrated and refactored code to the controller.