Fix TypeError due to race condition