Fix query logic