Updated code
Updated code to remove race condition