Refactor for templates code