fixed bug in query building