separated out the connection() logic