Made some changes to speed up things and also to separate the control code from tdtool code