Fixed a few bugs with the shutdown process.