slight cleanup of balance function, still fairly verbose