function for calculating balance terminated