fixed query logic