Refactor event handlers into extension.

Also wrap tests around the event handlers.
Could you add a comment here that these handlers are being provided by the events extension so that we can find them later :)