A Flexible State Machine