refactored clustering and bug fixes