improvement to implicit weighting code from Guillaume Englert