Moved connection setup to its own function.