Bug fix in query build.