Back to the original balancing code, have now found what looks like a compiler bug and worked around it