Moved connection logic to be more modular