Cleaned up the getting-the-original-query function.