Thread-safety fixes