The balancing algorithm