fix query logic