Refactored to a cleaner, event-based approach.