Refactored deinitialize so its thread safe