memoization: fixed broken test and reapplied all commits squashed in one