Fixed issue where extra lines of garbage appeared in the output file