Fix bugs in device discovery procedure