Fix concurrency issues when trying to use the library from multiple threads.