Added event API, cleanup and some bugfixes