Refactoring the query controller.