memoization
some tweaks to get full grade