implemented the caching and calculation functions