refactor gcd() a little bit (no functional changes).