Another speedup; cache these to go from O(N^2) to O(N).