Fix race conditition