Why did you place iteration over ranges inside iteration over errors? I think these 2 cycles should be run one after another.
Also, I would have renamed ranges into range_errors