Added memoization and other stuff