try to make it thread-safe