more thead safety fixes