Working code for selecting devices