Improvement: eliminated avg_hd loop