added gcd function and modified helper functions as necessary (everything still untested)