find and fix bugs in clustering logic