Refactor connection to prepare for reuse