Initial untested working version of state machine and matching