change thread join strategy, fix a few leaks, fix a few bugs