Simplified code and removed some more explicit caching.