Simplified the thread-safe section