added state saving and restoration code