fix query building