Cleaned up disconnect code; adding initial unit tests.