fixes for mulit-threaded version