                             } else {
                                 assert(false)
                             }
-                        }
+                        } // for expression
 
                         if eError {
                             syntaxErrors += (eError!, index + jIndex)
-                            result = result + "{" + expression + "}"
+                            let remainingExpression = str[advance(str.startIndex, jIndex)..<str.endIndex]
+                            if operator {
+                                result = result + "{" + operator! + remainingExpression + "}"
+                            } else {
+                                result = result + "{" + remainingExpression + "}"
+                            }
+
                         } else {
                             // Process VarSpec
+                            if varCount == 0 {
+                                result += behavior.first
+                            } else {
+                                result += behavior.sep
+                            }
+                            var expanded = expandVarSpec(varName, modifier, prefixLength, behavior, values)
+                            result += expanded
                         }
                     } // varspec-list
 

                            }
                        }
                        // Process varspec-list
                        var eError: URITemplateError? = nil
                        var estate = ExpressionState.ScanningVarName
                        var varName = ""
                        var modifier: Character?
                        var prefixLength :Int32 = 0
                        var str = expression[advance(expression.startIndex, skipCount)..<expression.endIndex]
                        str = str.stringByReplacingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
                        var jIndex = 0

            }

            return (operator, error)
        }

        func expandVarSpec(varName: String, modifier: Character?, prefixLength :Int,
        behavior: Behavior, values: AnyObject) -> String {
            var result = ""

            if varName == "" {
                return result
            }

            var value: AnyObject?
            if let d = values as? Dictionary<String, AnyObject> {
                value = d[varName]
            } else if let d = values as? NSDictionary {
                value = d.objectForKey(varName)
            } else {
                value = values.objectForKey?(varName)
            }

            if let str = stringOfAnyObject(value!) {
                if behavior.named {
                    result += encodeLiteralString(varName)
                    if str == "" {
                        result += behavior.ifemp
                        return result
                    } else {
                        result += "="
                    }
                }
                if modifier == ":" && prefixLength < countElements(str) {
                    var prefix = str[str.startIndex ..< advance(str.startIndex, prefixLength)]
                    result += encodeStringWithBehaviorAllowSet(prefix, behavior.allow)

                } else {
                    result += encodeStringWithBehaviorAllowSet(str, behavior.allow)
                }

            } else {
                if modifier == "*" {
                    if behavior.named {
                        if let ary = value as? [AnyObject] {
                            var count = 0
                            for v in ary {
                                var str = stringOfAnyObject(v)
                                if !str {
                                    continue
                                }
                                if count > 0 {
                                    result += behavior.sep
                                }
                                result += encodeLiteralString(varName)
                                if str! == "" {
                                    result += behavior.ifemp
                                } else {
                                    result += "="
                                    result += encodeStringWithBehaviorAllowSet(str!, behavior.allow)

                                }
                                ++count
                            }


                        } else if let dict = value as? Dictionary<String, AnyObject> {
                            var keys = Array(dict.keys)
                            keys = sorted(keys) {(s1: String, s2: String) -> Bool in
                                return s1.localizedCaseInsensitiveCompare(s2) == NSComparisonResult.OrderedAscending
                            }

                            var count = 0
                            for k in keys {
                                var str: String? = nil
                                if let v: AnyObject = dict[k] {
                                    str = stringOfAnyObject(v)
                                }
                                if !str {
                                    continue
                                }
                                if count > 0 {
                                    result += behavior.sep
                                }
                                result += encodeLiteralString(k)
                                if str == "" {
                                    result += behavior.ifemp
                                } else {
                                    result += "="
                                    result += encodeStringWithBehaviorAllowSet(str!, behavior.allow)
                                }
                                ++count
                            }

                        } else {
                            NSLog("Value for varName %@ is not a list or a pair", varName);
                        }

                    // end named
                    } else {
                        if let ary = value as? [AnyObject] {
                            var count = 0
                            for v in ary {
                                var str = stringOfAnyObject(v)
                                if !str {
                                    continue
                                }
                                if count > 0 {
                                    result += ","
                                }
                                result += encodeStringWithBehaviorAllowSet(str!, behavior.allow)
                                ++count
                            }

                        } else if let dict = value as? Dictionary<String, AnyObject> {
                            var keys = Array(dict.keys)
                            keys = sorted(keys) {(s1: String, s2: String) -> Bool in
                                return s1.localizedCaseInsensitiveCompare(s2) == NSComparisonResult.OrderedAscending
                            }

                            var count = 0
                            for k in keys {
                                var str: String? = nil
                                if let v: AnyObject = dict[k] {
                                    str = stringOfAnyObject(v)
                                }
                                if !str {
                                    continue
                                }
                                if count > 0 {
                                    result += ","
                                }
                                result += encodeLiteralString(k)
                                result += "="
                                result += encodeStringWithBehaviorAllowSet(str!, behavior.allow)
                                ++count
                            }

                        } else {
                            NSLog("Value for varName %@ is not a list or a pair", varName);
                        }
                    // end !named
                    }
                } else {

                }

            }
            return result
        }

        var state: State = .ScanningLiteral
