Adds extensive memoization

TODO: Occasionally check if the memo is getting too big and remove
infrequently-used entries.