         //bake the cookies
         var httpCookies: [HTTPCookie] = []
         for i in 0..<cookieIndices.count-1 {
-            if let aCookie = createHttpCookie(url: url, pairs: nameValuePairs, start: cookieIndices[i], end: cookieIndices[i+1]) {
+            if let aCookie = createHttpCookie(url: url, pairs: nameValuePairs[cookieIndices[i]..<cookieIndices[i+1]]) {
                 httpCookies.append(aCookie)
             }
         }

    } 

    //Bake a cookie
    private class func createHttpCookie(url: URL, pairs: [String], start: Int, end: Int) -> HTTPCookie? {
        var properties: [String:Any] = [:]
        for index in start..<end {
            let name = pairs[index].components(separatedBy: "=")[0]
            var value = pairs[index].components(separatedBy: "\(name)=")[1]  //a value can have an "="
            if canonicalize(name) == "Expires" {
                value = value.insertComma(at: 3)    //re-insert the comma   
            }

}

//utils for cookie parsing
fileprivate extension String {
    func trim() -> String {
        return self.trimmingCharacters(in: NSCharacterSet.whitespacesAndNewlines())
    }
