Move connection logic into its own file