Implemented the calculation algorithm