Saving and restoring state code complete