Implemented basic memoization.