balancing and settlement code