Fixed some bugs in the handling of events. Hopefully