Changed thread handling code