Added query and sorting functionality