added a bounded memoize function.