Completely reworked the way events are handled. This fixes issues caused by recent changes, and allows easier grouping of events.

Code runs and is (afaik) bugfree, but the work is not done: see #25