Explanation of calculation code to follow