fix some gcd memory bugs