improve discovery mechanism and add tests