Fix bug in StateMachine

Make time an unsigned long.
Improve thresholds.