ugly first pass on state machine