Refactor the way of handling templates