Fix switching of threads