memoization optimization.