I'll be happy if you have a simpler algorithm that does what we want and is efficient with a lots of contacts :)

This is not _so_ complicated though. Of course all this should be better done in Gecko, but since we won't have it in v1...