cleaning up query function