refactored query creation