almost done with rebalancing code