Started refactoring connection code