fixed writing back config when error happens