Fixed bugs in query builder