redesigned query functionality to improve performance