                                         outputFilePacketPosition,
                                         &frameCount,
                                         buffer.mData!)
-//            IF_ERR_GOTO_CLEANUP_1
+            if err != noErr {
+                cleanup1(outputBuffer: outputBuffer, sourceExtAudioFile: sourceExtAudioFile, forwardAudioFile: forwardAudioFile)
+                return err
+            }
+
             outputFilePacketPosition += Int64(frameCount)
         }
         
     }
     
 //    cleanup1:
-    
-    free(outputBuffer)
-    ExtAudioFileDispose(sourceExtAudioFile!)
-    AudioFileClose(forwardAudioFile!)
-//    IF_ERR_RETURN
+    cleanup1(outputBuffer: outputBuffer, sourceExtAudioFile: sourceExtAudioFile, forwardAudioFile: forwardAudioFile)
     
     // open the forward file for reading
     err = AudioFileOpenURL(forwardURL,
                            .readPermission,
                            kAudioFileCAFType,
                            &forwardAudioFile)
-//    IF_ERR_RETURN
+    if err != noErr { return err }
 
     // open the backward file for writing
     var backwardAudioFile: AudioFileID? = nil

                                    packetsProcessed,
                                    &packetsToTransfer,
                                    transferBuffer);
//        IF_ERR_GOTO_CLEANUP_2

        // log that we're actually working, every 1KB.
        if (packetsProcessed % 0x1000 == 0) {

    }
    
//    cleanup2:
    cleanup2(transferBuffer: transferBuffer, swapBuffer: swapBuffer, forwardAudioFile: forwardAudioFile, backwardAudioFile: backwardAudioFile)
    
    return err
}

fileprivate func cleanup1(outputBuffer: UnsafeMutableRawPointer?, sourceExtAudioFile: ExtAudioFileRef?, forwardAudioFile: AudioFileID?) {
    if let outputBuffer = outputBuffer { free(outputBuffer) }
    if let sourceExtAudioFile = sourceExtAudioFile { ExtAudioFileDispose(sourceExtAudioFile) }
    if let forwardAudioFile = forwardAudioFile { AudioFileClose(forwardAudioFile) }
}

fileprivate func cleanup2(transferBuffer: UnsafeMutableRawPointer?, swapBuffer: UnsafeMutableRawPointer?,
                          forwardAudioFile: AudioFileID?, backwardAudioFile: AudioFileID?) {
    
    if let transferBuffer = transferBuffer { free(transferBuffer) }
    if let swapBuffer = swapBuffer { free(swapBuffer) }
    if let forwardAudioFile = forwardAudioFile { AudioFileClose(forwardAudioFile) }
    if let backwardAudioFile = backwardAudioFile { AudioFileClose(backwardAudioFile) }
}
