Moved the code around to prepare for caching of the results