Refactored controller slightly