Fix bugs in unregister.  Write test code for unregister