Added a check if type is availible and removed keys from each loop in handle_device.