Add clustering functionality

This is a replacement PR of the work done in #1903.

It includes a clean history of the commits by @kenny806 and @fredj.

This can be merged once we are clear on the CLA status of @kenny806.