Fix threading errors