refactor template & improve purity