Externalize event handling and clean up