Fixed bug in advanced queries