Made the api thread-safe.