refactored query