Fix threading issues