add mutex and removed 2 bugs