wrote state machine