Improve code to remove duplicated templates