TermTest and some optimizations