Introduce core.memoize and refactor env