Refactor event handlers into extension.

Also wrap tests around the event handlers.