Added time switch code using interrupts.
Refactoring and cleanup
Simplified event management.