Fix cache race

An additional advantage of this is that in the changed code, if freezer remains nil, then `cache[mod]` will never be set, so anytime `mod` is looked up in the cache it will continue to do all this work. With the change I outlined above, the work will only ever be done once (except when being called in multiple threads of course, but then it will at least be minimized).