Fix race-condition's bugs