Fix some templating problem and remove duplication