Refactoring TH code