Refactor rebalance-related codes