Add state machine and adapt processing to the new states