Sync: Lots of bug fixes to discovery and listener.

Related to issue #418.