Fixed a bug that prevented clean shutdown