Added comments. Migrated functionality for inverse mod to extended_gcd function instead of the one I stole from the interwebs.