Added some shutdown logic to try to get things to garbage collect properly.