Move some connect code in to a function. Simplify code.