Implemented a new more improved query function