corrected the calculation of balance