optimized gcd stuff