cleaning up mergeTemplate function