Refactored memoization