Fixing a thread bug