Code for calculating the final result