Extension of Knuth's balancing algorithm with error correction