restructuring code for learning termination rule