refactored controller code