Optimize code through memoization

Soooo much faster now!