Restructuring the TH code + comments