Fixed a typo and moved the filename section up to where the file will be created.