Implements saving and loading of state with error handling