Fixed various bugs related to shutting down devices.