I am not that good with threads, but if this works better that is ok. I do think that these unrelated improvements should go into a separate pull though, it is quite a complex patch already.