query building bugfix