multithaeding fixes

tnx too ammar2 for multitraeding fixes for my patch
everything is ok to merge now