Mutex added. Solution rearranged.