Refactored event handling