queries refactoring