fixed thread killing functions and implemented waitpi(-1)