Propogate connection errors properly. Some refactoring.