Smarter memoisation

Only cache return values if error free.