Refactoring controller along with tests.