     }
 
     func requiredMonthlyPayment() -> Float {
+      //FIXME: Highly unefficient
         //    M = P[i(1+i)^n]/[(1+i)^n -1]
         return (amount * ((monthlyInterestRate * (1 + monthlyInterestRate)^^months) / ((1 + monthlyInterestRate)^^months - 1)))
     }

while (mortgage.balance > 0) {
    let extraPayment = (month % 12 == 0) ? monthlyExtraPayment + anuallyExtraPayment : monthlyExtraPayment

    let payment = mortgage.nextPayment(extraPayment: extraPayment)
    print("Payment : \(currencyFormatter.string(from: payment.principal)) + \(currencyFormatter.string(from: payment.interest)) = \(currencyFormatter.string(from: payment.total))")
    mortgage.applyPayment(payment: payment)
    print("\(month.toYearAndMonths()) --- Balance is \(currencyFormatter.string(from: mortgage.balance)) \n")
    month = month+1
}

        let totalPayment = requiredMonthlyPayment() * Float(months)
        return totalPayment - amount;
    }
}

//MARK: Mortgage Payment

extension Mortgage {
  func next(extraPayment: Float) -> MortgagePayment {
    let requiredPayment = requiredMonthlyPayment()
    if (requiredPayment > balance) {
      return MortgagePayment(principal: balance, interest: 0)
    }

    let maximumPossiblePayment = min(requiredPayment+extraPayment, balance)
    let interestDue = balance * monthlyInterestRate
    return MortgagePayment(total: maximumPossiblePayment, interest: interestDue)
  }

  func apply(payment: MortgagePayment) {
    balance -= payment.principal
    actualInterestPaid += payment.interest
  }
}


