added state preservation logic