improve query formulation algorithm