Refactor template loading