finished the balance function