fixed bugs related to saving state