refactoring event handling