refactor template code