Added basic undo support
Fixed bugs with undo stack and machine state