modification to Euclidean algorithm