fixed bugs in query