Improved algo to calculate degree