fix
switch to mutex
fix mutex locking to avoid race conditions