Fixed bug in query