Decompose distinct

Depends on #317 , review after that one.