more complex but hopefully better handling of clustering