Thread safety fixes