terminates thread; code is ugly