query refactored