Refactoring controller method, improving tests