refactored memoize