Fix possible race condition