Fixed and testsed state saving and loading.