Fixed calculation algorithm and improved test coverage