fix the balance function