Upgraded grade calculation fnction