Add support for callbacks and events, also some code cleanup