Refactored saving of the context.