Implement the state saving.