Moved query logic into model and out of controller.