Rescale algorithm rewritten