Fixes issue with undoing array insertions and deletions
Cleans up code in undo and redo functions