implementation of cluster distribution

various optimizations as well