Adjusted to use simpler version of GCD