Refactored controller logic out for clarity