fixed bug in threadsafe code