Refactored event handling.