fixed bug with multi-threaded version