Making the thread thread-safe