refactored density function for easier testability
optimization, get_best_clusters() is faster, shorter and easier to understand