Fix threadsafety bug