refactoring event management code