writing file without converting to string first plus cleanup