Added caching for some functions which use the complex algorithm.