Fixes undo stack

Fixes #1387