refactored, assorted fixes
Added event handling framework