rough code - broker and state machine function