cleanup
more cleanup and reapply history replace state