Hacked threading. Should switch to Boost or C++11