Refactor template logic.