Fix bdiscovery bug.
Refactor some