Fix threading problems