memoizing version