Refactored controller code