query refactor