Move clustering logic to metadata.

Close #1297.
Augh not a fan of the function name