Refactored to handle events