Refactored query