memoization refactoring