Add a new unit test for the connectivity layer. Fix two race conditions exposed by the unit tests. Fix several style issues.