removed error calculating substr; modified final_state_reached