Majorly edited source code to add a template feature.