Refacto reduce_ranges for performance