Use mutexes to avoid race conditions