Restructure main function
Add watchdog timer for external interrupts

After facing some deadlocks, add watchdog timers to all state changes caused by
external interrupts.