Optimize cycling in get_layer_range()