code cleanup, added undo feature