refactored event handling