Added unit tests for checking sub/unsub leaks.

Also made minor internal and cosmetic changes.