Refactored code to filter for the first query in the controller