added memoization code