Added a mutex to end race condition