Better solution to race condition