Implemented better query()