added some debugging, and reordered some shutdown logic