Resolve empty stack exception for undo and redo