Consolidated query logic