                 }
             }
         }
-        serverConfig.enabledOptions = options
         
         if let nameString = root[ConfigurationPropertyKey.nameKey] as? String {
             serverConfig.name = nameString
         }
+        
+        var threshold: Threshold?
+        if let thresholdsDict = jsonDictionary[ConfigurationPropertyKey.thresholdsKey] as? [String : AnyObject] {
+            let bg_high = thresholdsDict[ConfigurationPropertyKey.bg_highKey] as! Double
+            let bg_low = thresholdsDict[ConfigurationPropertyKey.bg_lowKey] as! Double
+            let bg_target_bottom = thresholdsDict[ConfigurationPropertyKey.bg_target_bottomKey] as! Double
+            let bg_target_top = thresholdsDict[ConfigurationPropertyKey.bg_target_topKey] as! Double
+            threshold = Threshold(bg_high: bg_high, bg_low: bg_low, bg_target_bottom: bg_target_bottom, bg_target_top: bg_target_top)
+        }
+
         
         var defaultsDefaults: Defaults?
         if let defaultsDictionary = root[ConfigurationPropertyKey.defaultsKey] as? [String: AnyObject] {

    static let defaultsKey = "defaults"
    static let alarmHighKey = "alarmHigh"
    static let alarmLowKey = "alarmLow"
    static let alarmTimeAgoUrgentKey = "alarmTimeAgoUrgent"
    static let alarmTimeAgoUrgentMinsKey = "alarmTimeAgoUrgentMins"
    static let alarmTimeAgoWarnKey = "alarmTimeAgoWarn"
    static let alarmTimeAgoWarnMinsKey = "alarmTimeAgoWarnMins"
    static let alarmUrgentHighKey = "alarmUrgentHigh"
    static let alarmUrgentLowKey = "alarmUrgentLow"
    static let customTitleKey = "customTitle"

            let alarms = Alarm(alarmHigh: aHigh, alarmLow: aLow, alarmTimeAgoUrgent: aTAU, alarmTimeAgoUrgentMins: aTAUMin, alarmTimeAgoWarn: aTAW, alarmTimeAgoWarnMins: aTAWMin, alarmUrgentHigh: aTUH, alarmUrgentLow: aTUL)
            
            let language = defaultsDictionary[ConfigurationPropertyKey.languageKey] as! String
        
            defaultsDefaults = Defaults(units: units, timeFormat: timeFormat, nightMode: nightMode, showRawbg: showRawbg, customTitle: customTitle, theme: theme, alarms: alarms, language: language, showPlugins: nil, enable: nil, thresholds: nil, defaultFeatures: nil)
        }
        
        if let settingsDictionary = root[ConfigurationPropertyKey.settingsKey] as? [String: AnyObject] {
            let units = Units(rawValue: (settingsDictionary[ConfigurationPropertyKey.unitsKey] as! String).lowercaseString)!
            let timeFormat = (settingsDictionary[ConfigurationPropertyKey.timeFormatKey] as! String).toInt()!
            let nightMode = settingsDictionary[ConfigurationPropertyKey.nightModeKey] as! Bool
            let showRawbg = RawBGMode(rawValue: settingsDictionary[ConfigurationPropertyKey.showRawbgKey] as! String)!
            let customTitle = settingsDictionary[ConfigurationPropertyKey.customTitleKey] as! String
            
            let theme = settingsDictionary[ConfigurationPropertyKey.themeKey] as! String
            
            let aHigh = settingsDictionary[ConfigurationPropertyKey.alarmHighKey] as! Bool
            let aLow = settingsDictionary[ConfigurationPropertyKey.alarmLowKey] as! Bool
            let aTAU = settingsDictionary[ConfigurationPropertyKey.alarmTimeagoUrgentKey] as! Bool
            let aTAUMDouble = settingsDictionary[ConfigurationPropertyKey.alarmTimeagoUrgentMinsKey] as! Double
            let aTAUMin: NSTimeInterval = aTAUMDouble * 60 // Convert minutes to seconds.
            
            let aTAW = settingsDictionary[ConfigurationPropertyKey.alarmTimeagoWarnKey] as! Bool
            let aTAWMDouble = settingsDictionary[ConfigurationPropertyKey.alarmTimeagoWarnMinsKey] as! Double
            let aTAWMin: NSTimeInterval = aTAWMDouble * 60 // Convert minutes to seconds.
            let aTUH = settingsDictionary[ConfigurationPropertyKey.alarmUrgentHighKey] as! Bool
            let aTUL = settingsDictionary[ConfigurationPropertyKey.alarmUrgentLowKey] as! Bool
            
            let alarms = Alarm(alarmHigh: aHigh, alarmLow: aLow, alarmTimeAgoUrgent: aTAU, alarmTimeAgoUrgentMins: aTAUMin, alarmTimeAgoWarn: aTAW, alarmTimeAgoWarnMins: aTAWMin, alarmUrgentHigh: aTUH, alarmUrgentLow: aTUL)
            
            let language = settingsDictionary[ConfigurationPropertyKey.languageKey] as! String
            
            
            if let enableArray = settingsDictionary[ConfigurationPropertyKey.enableKey] as? [String] {
                for stringItem in enableArray{
                    if let item = EnabledOptions(rawValue: stringItem){
                        options.append(item)
                    }
                }
            }
            
            if let thresholdsDict = settingsDictionary[ConfigurationPropertyKey.thresholdsKey] as? [String : AnyObject] {
                let bg_high = thresholdsDict[ConfigurationPropertyKey.bgHighKey] as! Double
                let bg_low = thresholdsDict[ConfigurationPropertyKey.bgLowKey] as! Double
                let bg_target_bottom = thresholdsDict[ConfigurationPropertyKey.bgTargetBottomKey] as! Double
                let bg_target_top = thresholdsDict[ConfigurationPropertyKey.bgTargetTopKey] as! Double
                threshold = Threshold(bg_high: bg_high, bg_low: bg_low, bg_target_bottom: bg_target_bottom, bg_target_top: bg_target_top)
            }


            
            defaultsDefaults = Defaults(units: units, timeFormat: timeFormat, nightMode: nightMode, showRawbg: showRawbg, customTitle: customTitle, theme: theme, alarms: alarms, language: language, showPlugins: nil, enable: options, thresholds: threshold, defaultFeatures: nil)

        }

        serverConfig.defaults = defaultsDefaults
        
        serverConfig.thresholds = threshold
        serverConfig.enabledOptions = options

        self = serverConfig
    }
}
