Working version with outfile = infile