Rewrote connections to use a state machine