Be smarter about capturing the environment for memoized code