Controller refactoring and tests