fixed query formulation bugs