Added destructor to allow the thread to end before being destroyed, preventing crash when shutting down.