   func fromUnitSelected(fromUnit: Dictionary<String,Dimension>) {
     if let measurement = self.dataObject {
       let defaultUnits = defaults.dictionary(forKey: measurement.header)
-      let toUnit = getUnit(unitName: defaultUnits?["toUnit"] as! String, measurement: measurement)
-      self.defaults.set(["fromUnit": fromUnit.keys.first, "toUnit": toUnit.keys.first], forKey: self.dataLabel!.text!)
+      self.defaults.set(["fromUnit": fromUnit.keys.first!, "toUnit": defaultUnits?["toUnit"] as! String], forKey: self.dataLabel!.text!)
       let fromUnitName = fromUnit.keys.first
       self.fromButton.setTitle(fromUnitName, for: .normal)
       self.selectedFromUnit = fromUnit[fromUnitName!]

  }

  @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)
      
      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.triggerFromValueChange()
    }
  }

    }
  }

  func getUnit(_ units: [Dictionary<String,Dimension>], unitName: String) -> Dictionary<String,Dimension>? {
    let foundUnits = units.filter({(unit: Dictionary<String,Dimension>) -> Bool in
      if let key = unit.keys.first {
        return key == unitName
      } else {
        return false
      }
    })
    return foundUnits.first
  }

  func getUnit(unitName: String, measurement: MyMeasurement) -> Dictionary<String, Dimension>? {
    if let foundSIUnit = getUnit(measurement.SIValues, unitName: unitName) {
      return foundSIUnit
    } else {
      return getUnit(measurement.imperialValues, unitName: unitName)
    }
  }

