Fixed handling of errors during initialize and shutdown