Fix algorithm of calculation