improving the rebalance code

Making rebalance more efficient and more likely to succeed