fixed undo bug, and re-structured event handling code so much more STABLE