Refactoring of common code. Caching of last json for debugging.