Implemented extended euclid algorithm