solved some bugs in query