Allows templateFile and outputFile to be the same file.
Adds null checks