refactoring query