         }
         
         // compute data subchunk size (excludes id and size fields)
-        var dataSubchunkSize = UInt32(data.length - fieldOffset - fieldSize)
+        var dataSubchunkSize = UInt32(data.count - fieldOffset - fieldSize)
         
         // update data subchunk size
-        let dataSubchunkSizeRange = NSMakeRange(fieldOffset, fieldSize)
-        data.replaceBytes(in: dataSubchunkSizeRange, withBytes: &dataSubchunkSize)
+        let dataSubchunkSizeData = Data(bytes: &dataSubchunkSize, count: MemoryLayout<UInt32>.stride)
+        data.replaceSubrange(Range(uncheckedBounds: (lower: fieldOffset, upper: fieldOffset+fieldSize)), with: dataSubchunkSizeData)
     }
 }

                case .none:
                    if audioFormat == .wav {
                        // repair the WAV header
                        let wav = NSMutableData(data: data)
                        guard TextToSpeech.isWAVFile(data: wav) else {
                            let failureReason = "Returned audio is in an unexpected format."
                            let userInfo = [NSLocalizedFailureReasonErrorKey: failureReason]

     - returns: An Int initialized by converting the given little-endian byte buffer into
            an unsigned 32-bit integer.
     */
    private static func dataToUInt32(data: Data, offset: Int) -> Int {
        var num: UInt8 = 0
        let length = 4
        let range = Range(uncheckedBounds: (lower: offset, upper: offset + length))
        data.copyBytes(to: &num, from: range)
        return Int(num)
    }
    
