Refactoring query logic to allow multiple queries