Change the way events are registered and callbacks are handled.

This fixes issue #13