Fixing nasty race condition