added basic balancing code