Fixed rebalance bugs & added test