Adding code for balanced partitioning