implement tewithink, clean up code