Change to state machine architecture, add test mode.