add Template and refactor legacy functions