Restructuring the TS code