Changed weighted_average function for better mode handling