   }
 
   @IBAction func onSwitchButtonClicked(_ sender: Any) {
-    if let measurement = self.dataObject {
-      let formerFromValues = self.fromValues
-      let formerSelectedFromUnit = self.selectedFromUnit
-      let formerToValues = self.toValues
-      let formerSelectedToUnit = self.selectedToUnit
-      let defaultUnits = defaults.dictionary(forKey: self.dataLabel!.text!)
-      let formerFromUnit = getUnit(unitName: defaultUnits?["fromUnit"] as! String, measurement: measurement)
-      let formerToUnit = getUnit(unitName: defaultUnits?["toUnit"] as! String, measurement: measurement)
-      
+    let formerFromValues = self.fromValues
+    let formerSelectedFromUnit = self.selectedFromUnit
+    let formerToValues = self.toValues
+    let formerSelectedToUnit = self.selectedToUnit
+    if let defaultUnits = defaults.dictionary(forKey: self.dataLabel!.text!) {
+      let formerFromUnitName = defaultUnits["fromUnit"] as! String
+      let formerToUnitName = defaultUnits["toUnit"] as! String
       self.fromValues = formerToValues
       self.toValues = formerFromValues
       self.selectedFromUnit = formerSelectedToUnit
       self.selectedToUnit = formerSelectedFromUnit
-      self.defaults.set(["fromUnit": formerToUnit.keys.first, "toUnit": formerFromUnit.keys.first], forKey: self.dataLabel!.text!)
-      self.fromButton.setTitle(formerToUnit.keys.first, for: .normal)
-      self.toButton.setTitle(formerFromUnit.keys.first, for: .normal)
+      self.defaults.set(["fromUnit": formerToUnitName, "toUnit": formerFromUnitName], forKey: self.dataLabel!.text!)
+      self.fromButton.setTitle(formerToUnitName, for: .normal)
+      self.toButton.setTitle(formerFromUnitName, for: .normal)
       self.triggerFromValueChange()
     }
   }

  public func toUnitSelected(toUnit: Dictionary<String,Dimension>) {
    if let measurement = self.dataObject {
      let defaultUnits = defaults.dictionary(forKey: measurement.header)
      let fromUnit = getUnit(unitName: defaultUnits?["fromUnit"] as! String, measurement: measurement)
      self.defaults.set(["fromUnit": fromUnit.keys.first, "toUnit": toUnit.keys.first], forKey: self.dataLabel!.text!)
      let toUnitName = toUnit.keys.first
      self.toButton.setTitle(toUnitName, for: .normal)
      self.selectedToUnit = toUnit[toUnitName!]

      var toUnit: Dictionary<String,Dimension> = [:]
      if let defaultUnits = defaults.dictionary(forKey: measurement.header) {
        let fromUnitName = defaultUnits["fromUnit"] as! String
        let toUnitName = defaultUnits["toUnit"] as! String
        if let fUnit = getUnit(unitName: fromUnitName, measurement: measurement), let tUnit = getUnit(unitName: toUnitName, measurement: measurement) {
          fromUnit = fUnit
          toUnit = tUnit
        } else {
          fromUnit = measurement.SIValues[0]
          toUnit = measurement.imperialValues[0]
          self.defaults.set(["fromUnit": fromUnit.keys.first, "toUnit": toUnit.keys.first], forKey: measurement.header)
        }
      } else {
        fromUnit = measurement.SIValues[0]
        toUnit = measurement.imperialValues[0]
