move common code into connection