further tweaks on onterminate code