make saving of state atomic