Add a lock to make it thread safe