             var current_hr: Double = -1;
             let bpm_unit = HKUnit(from: "count/min")
             var hr_string = "";
-            
-            print("<?xml version=\"1.0\" encoding=\"UTF-8\"?><gpx version=\"1.1\" creator=\"Apple Workouts (via pilif's hack of the week)\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"><trk><name><![CDATA[\(workout_name) - \(self.dateFormatter.string(from: workout.startDate))]]></name><time>\(iso_formatter.string(from: workout.startDate))</time><trkseg>", terminator:"")
+            file.write(
+                "<?xml version=\"1.0\" encoding=\"UTF-8\"?><gpx version=\"1.1\" creator=\"Apple Workouts (via pilif's hack of the week)\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/1\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\" xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"><trk><name><![CDATA[\(workout_title)]]></name><time>\(iso_formatter.string(from: workout.startDate))</time><trkseg>"
+                        .data(using: .utf8)!
+            )
 
             self.workoutStore.route(for: workouts[indexPath.row]){
                 (maybe_locations, done, error) in
                 guard let locations = maybe_locations, error == nil else {
                     print(error as Any);
+                    file.closeFile()
                     return
                 }
-                
+
                 for location in locations {
                     while (current_heart_rate_index < keyedRates.count) && (location.timestamp > keyedRates[current_heart_rate_index].startDate) {
                         current_hr = keyedRates[current_heart_rate_index].quantity.doubleValue(for: bpm_unit)

                        hr_string = "<extensions><gpxtpx:TrackPointExtension><gpxtpx:hr>\(current_hr)</gpxtpx:hr></gpxtpx:TrackPointExtension></extensions>"
                    }

                    print("<trkpt lat=\"\(location.coordinate.latitude)\" lon=\"\(location.coordinate.longitude)\"><ele>\(location.altitude.magnitude)</ele><time>\(iso_formatter.string(from: location.timestamp))</time>\(hr_string)</trkpt>", terminator:"")
                }
                if (done){
                    print("</trkseg></trk></gpx>")
                }
            }
            

                default: return "Workout"
            }
        }()
        let workout_title = "\(workout_name) - \(self.dateFormatter.string(from: workout.startDate))"

        let targetURL = URL(fileURLWithPath: NSTemporaryDirectory())
            .appendingPathComponent(workout_title)
            .appendingPathExtension("gpx")

        let file: FileHandle

        do {
            let manager = FileManager.default;
            if manager.fileExists(atPath: targetURL.path){
                try manager.removeItem(atPath: targetURL.path)
            }
            print(manager.createFile(atPath: targetURL.path, contents: Data()))
            file = try FileHandle(forWritingTo: targetURL);
        }catch let err {
            print(err)
            return
        }

        workoutStore.heartRate(for: workouts[indexPath.row]){
            (rates, error) in
