rebalance.m not pretty, but working