Fixed bugs in clustering code