Refactor event code

Separate most of the event-related from registry package for testability and readability.