 
     private func upload(forChunks chunks: [Data], withUpload upload: TUSUpload, atPosition position: Int, completion: @escaping (Bool) -> Void) -> URLSessionUploadTask {
         TUSClient.shared.logger.log(forLevel: .Info, withMessage: String(format: "Upload starting for file %@ - Chunk %u / %u", upload.id, position + 1, chunks.count))
-
-        // TODO: Retry-Mechanism: The places where we called `markAsFailed` are the places where we could retry uploading the failed chunk.
-        func markAsFailed(upload: TUSUpload, error: Error?) {
-            cancel(forUpload: upload, error: error, failed: true)
-
-            if continueUploading() {
-                let pendingUploads = TUSClient.shared.pendingUploads()
-                TUSClient.shared.createOrResume(forUpload: pendingUploads[0])
-            } else {
-                completion(false)
-            }
-        }
 
         
         let request: URLRequest = urlRequest(withFullURL: upload.uploadLocationURL!, andMethod: "PATCH", andContentLength: upload.contentLength!, andUploadLength: nil, andFilename: upload.getUploadFilename(), andHeaders: ["Content-Type": "application/offset+octet-stream", "Upload-Offset": upload.uploadOffset!, "Content-Length": String(chunks[position].count), "Upload-Metadata": upload.encodedMetadata])

            }
            
            // Do the work:
            if (skipResumeCheck) {
                let data = upload.getData()
                let chunks = self.dataIntoChunks(data: data, chunkSize: TUSClient.shared.chunkSize)
                startUpload(chunks: chunks)
            } else {
                self.prepareUpload(forUpload: upload) { (chunks, skipUploadMarkSuccess) in
                    if (skipUploadMarkSuccess) {
                        self.handleUploadSuccess(upload: upload, completion: uploadFinishedCallback)
                    } else {
                        startUpload(chunks: chunks)
                    }
                }
            }
        }
    }

        let pendingUploads = TUSClient.shared.pendingUploads()
        return pendingUploads.count > 0 && (uploadWholeQueueInBackground || !isAppBackground)
    }

    // TODO: Retry-Mechanism: The places where we called `markAsFailed` are the places where we could retry uploading the failed chunk.
    internal func markAsFailed(upload: TUSUpload, completion: @escaping (Bool) -> Void, error: Error?) {
        cancel(forUpload: upload, error: error, failed: true)

        if continueUploading() {
            let pendingUploads = TUSClient.shared.pendingUploads()
            TUSClient.shared.createOrResume(forUpload: pendingUploads[0])
        } else {
            completion(false)
        }
    }
}
