Handle thread termination properly - RaspberryPi is still locking up sometimes.

Suspect memory error or possibly race condition.