replaced division by n with division by sum of the weights