added some pthread cleanup and signal handling