fixing saving state and restoring state