Improved the weighting algorithm