refactoring in connection code; general clean up