fix bugs and enhanced templating