Refactored event handler