Correct range trimming algorithm