Fix the race condition and use isEmpty instead of size