corrected balance function