Fixed the calculation code at last