Faster with memoization, (This is code taken from notes, day 14)