 }
 
 func checkKey(key: String) {
-    if smc.isKeyValid(key).valid { print("VALID")   }
-    else                         { print("INVALID") }
-}
-
-func setMinFanSpeed(fanNumber: Int, fanSpeed: Int) {
-    let result = smc.setFanMinRPM(UInt(fanNumber), RPM: UInt(fanSpeed))
-
-    if result.result { print("SUCCESS") }
-    else if result.IOReturn == kIOReturnNotPrivileged {
+    if key.characters.count != 4 {
+        print("Must be FourCC")
+        return
+    }
+
+    do {
+        let isValid = try SMCKit.isKeyValid(FourCharCode(fromString: key))
+
+        if isValid { print("VALID")   }
+        else       { print("INVALID") }
+    } catch { print(error) }
+}
+
+func setMinFanSpeed(fanId: Int, fanSpeed: Int) {
+    do {
+        try SMCKit.fanSetMinSpeed(fanId, speed: fanSpeed)
+        print("SUCCESS")
+    } catch SMCKit.Error.KeyNotFound {
+        print("This machine has no fan with id \(fanId)")
+    } catch SMCKit.Error.NotPrivileged {
         print("This operation must be invoked as the superuser")
-    }
-    else if result.IOReturn == kIOReturnBadArgument {
-        let maxSpeed = smc.getFanMaxRPM(UInt(fanNumber)).rpm
-        print("Invalid fan speed. Must be <= max fan speed (\(maxSpeed))")
-    }
-    else if result.kSMC == SMC.kSMC.kSMCKeyNotFound.rawValue {
-        print("This machine has no fan #\(fanNumber)")
-    }
-    else {
-        print("FAILED: IOKit(\(result.IOReturn)), SMC(\(result.kSMC))")
+    } catch SMCKit.Error.UnsafeFanSpeed {
+        print("Invalid fan speed. Must be <= max fan speed")
+    } catch {
+        print(error)
     }
 }
 


func printTemperatureInformation() {
    print("-- TEMPERATURE --")
    let temperatureSensors = smc.getAllValidTemperatureKeys()

    if temperatureSensors.count == 0 {
        print("No known temperature sensors found")
        return
    }


    let sensorWithLongestName = temperatureSensors.maxElement
                            { SMC.Temperature.allValues[$0]!.characters.count <
                              SMC.Temperature.allValues[$1]!.characters.count }

    let longestSensorNameCount =
             SMC.Temperature.allValues[sensorWithLongestName!]!.characters.count


    for key in temperatureSensors {
        var temperatureSensorName = SMC.Temperature.allValues[key]!
        let temperature           = smc.getTemperature(key).tmp


        // Padding to line up temperatures
        let padding = longestSensorNameCount -
                      temperatureSensorName.characters.count
        for var i = 0; i < padding; ++i { temperatureSensorName.extend(" ") }


        let warning = warningLevel(temperature, maxValue: maxTemperatureCelsius)
        let level   = CLIWarnOption.value ? "(\(warning.name))" : ""
        let color   = CLIColorOption.value ? warning.color : ANSIColor.Off

        let smcKey  = CLIDisplayKeysOption.value ? "(\(key.rawValue))" : ""

        print("\(temperatureSensorName)   \(smcKey)  ", appendNewline: false)
        print("\(color.rawValue)\(temperature)°C \(level)" +
                                "\(ANSIColor.Off.rawValue)")
    }
}

func printFanInformation() {
    print("-- FAN --")
    let fanCount = smc.getNumFans().numFans

    if fanCount == 0 { print("** Fanless **") }
    else {
        for var i: UInt = 0; i < fanCount; ++i {
            let name    = smc.getFanName(i).name
            let current = smc.getFanRPM(i).rpm
            let min     = smc.getFanMinRPM(i).rpm
            let max     = smc.getFanMaxRPM(i).rpm

            let warning = warningLevel(Double(current), maxValue: Double(max))
            let level   = CLIWarnOption.value ? "(\(warning.name))" : ""
            let color   = CLIColorOption.value ? warning.color : ANSIColor.Off

            print("[id \(i)] \(name)")
            print("\tCurrent:  \(color.rawValue)\(current) RPM \(level)" +
                                                    "\(ANSIColor.Off.rawValue)")
            print("\tMin:      \(min) RPM")
            print("\tMax:      \(max) RPM")
        }
    }
}

func printPowerInformation() {
    print("-- POWER --")
    print("AC Present:       \(colorBoolOutput(smc.isACPresent().flag))")
    print("Battery Powered:  \(colorBoolOutput(smc.isBatteryPowered().flag))")
    print("Charging:         \(colorBoolOutput(smc.isCharging().flag))")
    print("Battery Ok:       \(colorBoolOutput(smc.isBatteryOk().flag))")
    print("Max Batteries:    \(smc.maxNumberBatteries().count)")
}

func printMiscInformation() {
    print("-- MISC --")

    let ODDStatus = smc.isOpticalDiskDriveFull().flag
    print("Disc in ODD:      \(colorBoolOutput(ODDStatus))")
}


if CLIPowerOption.value       { printPowerInformation()       }
if CLIMiscOption.value        { printMiscInformation()        }

SMCKit.close()
