query refactoring