Refactor TH code