         }
     }
 
-    private func ensureToken(callback: (ShareError?, String?) -> Void) {
-        if token != nil {
-            callback(nil, token)
-        } else {
-            fetchToken() { (error, token) in
-                if error != nil {
-                    callback(error, nil)
-                } else {
-                    self.token = token
-                    callback(nil, token)
-                }
-            }
-        }
-    }
-
-    private func fetchToken(callback: (ShareError?, String?) -> Void) {
-        let data = [
-            "accountName": username,
-            "password": password,
-            "applicationId": dexcomApplicationId
-        ]
-
-        guard let URL = NSURL(string: dexcomServerUS + dexcomLoginPath) else {
-            return callback(ShareError.FetchError, nil)
-        }
-
-        dexcomPOST(URL, JSONData: data) { (error, response) in
-            if let error = error {
-                return callback(ShareError.HTTPError(error), nil)
-            }
-
-            guard let   response = response,
-                        data = response.dataUsingEncoding(NSUTF8StringEncoding),
-                        decoded = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
-            else {
-                return callback(ShareError.LoginError(errorCode: "unknown"), nil)
-            }
-
-            if let token = decoded as? String {
-                // success is a JSON-encoded string containing the token
-                callback(nil, token)
-            } else {
-                // failure is a JSON object containing the error reason
-                let errorCode = (decoded as? [String: String])?["Code"] ?? "unknown"
-                callback(ShareError.LoginError(errorCode: errorCode), nil)
-            }
-        }
-    }
-
     private func parseDate(wt: String) throws -> NSDate {
         // wt looks like "/Date(1462404576000)/"
         let re = try NSRegularExpression(pattern: "\\((.*)\\)", options: NSRegularExpressionOptions())


                    let decoded = try? NSJSONSerialization.JSONObjectWithData(response.dataUsingEncoding(NSUTF8StringEncoding)!, options: NSJSONReadingOptions())
                    guard let sgvs = decoded as? Array<AnyObject> else {
                        throw ShareError.DataError(reason: "Failed to decode SGVs as array: " + response)
                    }

                    var transformed: Array<ShareGlucose> = []

    }

    public func fetchLast(n: Int, callback: (ShareError?, [ShareGlucose]?) -> Void) {
        fetchLastWithRetries(n, remaining: maxReauthAttempts, callback: callback)
    }

    private func ensureToken(callback: (ShareError?) -> Void) {
        if token != nil {
            callback(nil)
        } else {
            fetchToken() { (error, token) in
                if error != nil {
                    callback(error)
                } else {
                    self.token = token
                    callback(nil)
                }
            }
        }
    }

    private func fetchToken(callback: (ShareError?, String?) -> Void) {
        let data = [
            "accountName": username,
            "password": password,
            "applicationId": dexcomApplicationId
        ]

        guard let URL = NSURL(string: dexcomServerUS + dexcomLoginPath) else {
            return callback(ShareError.FetchError, nil)
        }

        dexcomPOST(URL, JSONData: data) { (error, response) in
            if let error = error {
                return callback(ShareError.HTTPError(error), nil)
            }

            guard let   response = response,
                data = response.dataUsingEncoding(NSUTF8StringEncoding),
                decoded = try? NSJSONSerialization.JSONObjectWithData(data, options: .AllowFragments)
                else {
                    return callback(ShareError.LoginError(errorCode: "unknown"), nil)
            }

            if let token = decoded as? String {
                // success is a JSON-encoded string containing the token
                callback(nil, token)
            } else {
                // failure is a JSON object containing the error reason
                let errorCode = (decoded as? [String: String])?["Code"] ?? "unknown"
                callback(ShareError.LoginError(errorCode: errorCode), nil)
            }
        }
    }

    private func fetchLastWithRetries(n: Int, remaining: Int, callback: (ShareError?, [ShareGlucose]?) -> Void) {
        ensureToken() { (error) in
            guard error == nil else {
                return callback(error, nil)
            }

