Device save/restore: handle non-existing keys and beautify code