added threading locks and other stuff