Threading fixes

Fixes for two threading issues.