Refactoring queries