thread-safety fixes