Fixes a bug with undo insert
Takes care of problems with undo-redo due to sorting
Minor refactoring for undo and redo functions
Merge branch 'jayant1992-undo-redo'