Abstract out previous GCD(m,n)=1 commit into a single function