Added support for events, fixed a couple of bugs