Refactoring controller logic