Fixed MAJOR bug in query