     switch factor {
     case .Timer(let period):
         urlComponents.host = FactorTimerString
-        urlComponents.queryItems.append(NSURLQueryItem(name:kQueryPeriodKey, value:String(Int(period))))
+        queryItems.append(NSURLQueryItem(name:kQueryPeriodKey, value:String(Int(period))))
     case .Counter(let counter):
         urlComponents.host = FactorCounterString
-        urlComponents.queryItems.append(NSURLQueryItem(name:kQueryCounterKey, value:String(counter)))
+        queryItems.append(NSURLQueryItem(name:kQueryCounterKey, value:String(counter)))
     }
+
+    urlComponents.queryItems = queryItems
 
     return urlComponents.URL
 }

    public struct URLSerializer: TokenSerializer {
        public static func serialize(token: Token) -> String? {
            let url = urlForToken(name: token.name, issuer: token.issuer, factor: token.core.factor, algorithm: token.core.algorithm, digits: token.core.digits)
            return url.absoluteString
        }

        public static func deserialize(string: String, secret: NSData? = nil) -> Token? {
            if let url = NSURL.URLWithString(string) {
                return tokenFromURL(url, secret: secret)
            }
            return nil

    return nil
}

private func urlForToken(#name: String, #issuer: String, #factor: Generator.Factor, #algorithm: Generator.Algorithm, #digits: Int) -> NSURL? {
    let urlComponents = NSURLComponents()
    urlComponents.scheme = kOTPAuthScheme
    urlComponents.path = "/" + name

    var queryItems = [
        NSURLQueryItem(name:kQueryAlgorithmKey, value:stringForAlgorithm(algorithm)),
        NSURLQueryItem(name:kQueryDigitsKey, value:String(digits)),
        NSURLQueryItem(name:kQueryIssuerKey, value:issuer)
