refactor; move state machine into separate namespace