Is this a valid proof that a finitely generated torsion-free module over a PID is free?

I made an effort to prove this theorem without using the structure theorem for modules over a PID (in fact, it is a part of my attempt to prove the structure theorem on my own).

After finishing the proof, I looked online/in books for a bit, and I haven't found anything similar to my argument, so I was wondering if the proof I have come up with is in fact valid.

Suppose $R$ is a PID, and $M$ is a finitely generated torsion-free module over $R$. Furthermore, suppose that $M$ can be generated by $n$ elements.

Theorem: $M$ is free of rank $k$ for some $k \leq n$.

Proof:

We do induction on $n$. For the case of $n=1$, we have $M=Ra$ for some $a \in M$. Since $M$ is torsion-free, it follows that $M \cong R$, and is thus free.

Now suppose $n>1$, and $M$ may be generated by some elements $a_1,\ldots,a_n$. (That is, $M=Ra_1 + \cdots + Ra_n$ ).

Then, consider the submodule $M'=Ra_1 + \cdots + Ra_{n-1}$. Then, $M'$ is torsion free because it is a submodule of $M$, and it can be generated by $n-1$ elements, so therefore by induction it must be free of rank $k \leq n-1$. Hence we may pick elements $e_1,e_2,\ldots,e_k \in M'$ which freely generate $M'$ (form a basis for it).

If $a_n \in M'$, then $M=M'$, so $M$ is free and we are done. Thus we may suppose $a_n \not \in M$.

Next, consider the set $Ra_n \cap M'$. If $Ra_n \cap M' = (0)$, then we have $M=Ra_n \oplus M'$ (because clearly M= Ra_n + M'). Thus, $M$ is a direct sum of two free modules, and is thus free.

Hence we may suppose $Ra_n \cap M' \neq (0)$. Thus there exists some $z \in Ra_n \cap M'$ such that $z \neq 0$. Thus, there exists some $r_n \in R$ such that $z=r_na_n$ and there exists $r_1,\ldots,r_k \in R$ such that $z=r_1e_1 + \cdots + r_ke_k$. Hence we have: $$r_na_n = r_1e_1 + \cdots + r_ke_k$$

Since $R$ is a PID, we may write $(r_1,\ldots,r_k)=(d)$ for some $d \in R$. Thus define $r'_i$ by $r_i=dr'_i$ for $i=1.,\ldots,k$. Thus we have: $$r_na_n = d (r'_1e_1 + \cdots r'_ke_k)$$

Where $(r'_1,\ldots,r'_k)=1$. Furthermore, since $M$ is torsion-free, we may assume $(r_n,d)=1$. Since $R$ is a PID, this means that (by bezouts theorem) there exists $f_1,f_2 \in R$ such that: $$f_1r_n + f_2d = 1$$

Now we define: $$x := f_1 (r'_1e_1+\cdots + r'_ke_k) + f_2a_n$$

Then we have:

$$r_nx = r_nf_1 (r'_1e_1+\cdots + r'_ke_k) + f_2r_na_n = r_nf_1 (r'_1e_1+\cdots + r'_ke_k) + f_2d (r'_1e_1+\cdots + r'_ke_k)$$ $$= (r_nf_1 + f_2d)(r'_1e_1+\cdots + r'_ke_k) = r'_1e_1+\cdots + r'_ke_k$$

and:

$$dx = f_1d(r'_1e_1+\cdots + r'_ke_k) + f_2da_n = f_1r_na_n + f_2da_n = (f_1r_n + f_2d)a_n = a_n$$

Thus, $a_n \in Rx$, so $M$ is generated by $x,e_1,\ldots,e_k$.

Now we pass to the quotient module $M/Rx$. Let $\bar{y}$ denote the image of some $y \in M$ under the natural projection $M \to M/Rx$.

Since $x,e_1,\ldots,e_k$ generate $M$, it follows that $\bar{e_1},\bar{e_2},\ldots,\bar{e_k}$ generate $M/Rx$.

Claim: $M$ is torsion-free.

Proof: Suppose $t(g_1\bar{e_1} + \cdots + g_k\bar{e_k})=0$ for some $t,g_1,\ldots,g_k \in R$ such that $t \neq 0$. Furthermore, by factoring out any common factors of the $g_i$ and relabeling $t$, we may assume w.l.o.g. that $(g_1,\ldots,g_k)=1$.

Since $t(g_1\bar{e_1} + \cdots + g_k\bar{e_k})= \overline{t(g_1e_1 + \cdots g_ke_k)}$, it follows that $t(g_1e_1 + \cdots g_ke_k) \in Rx$. Thus there exists $r \in R$ such that:

$$t(g_1e_1 + \cdots g_ke_k) = rx$$

Since $M$ is torsion-free, we may further suppose that $(t,r)=1$. Multiplying by $r_n$ gives:

$$r_nt(g_1e_1 + \cdots g_ke_k) = r(r'_1e_1 + \cdots r'_ke_k)$$

Since $e_1,\ldots,e_k$ is a basis for $M'$, it follows that we have: $$\tag{1} r_ntg_i = rr'_i$$ for $i=1,\ldots,k$.

Since $(g_1,\ldots,g_k)=1$, there exists $p_1,\ldots,p_k \in R$ such that $\sum p_ig_i = 1$. Likewise, since $(r'_1,\ldots,r'_k)=1$, there exists $h_1,\ldots,h_k$ such that $\sum h_ir'_i = 1$.

If we multiply $(1)$ by $p_i$ and sum over all $i=1,\ldots,k$, we get:

$$r_nt = r \sum p_ir'_i$$

Thus $r \mid r_nt$. Since $(r,t)=1$ and $R$ is a PID, it follows that $r \mid r_n$.

If we multiply $(1)$ by $h_i$ and sum over all $i=1,\ldots,k$, we get:

$$r_nt\sum h_ig_i = r$$

Thus $r_n \mid r$. Hence we can write $r_n=ur$ for some unit $u \in R$.

Combining the previous two equations, we get: $$r (\sum p_ir'_i)(\sum h_ig_i) = r$$

Since $R$ is an integral domain and $r \neq 0$, it follows that:

$$(\sum p_ir'_i)(\sum h_ig_i) = 1$$

From $r_nt = r \sum p_ir'_i$, we get $urt = r \sum p_ir'_i$. Canceling $r$ gives: $$t = u^{-1}\sum p_ir'_i$$

Thus $t$ is a unit, as it is a product of two units.

Thus, we have $t(g_1\bar{e_1} + \cdots + g_k\bar{e_k})=0$ implies $g_1\bar{e_1} + \cdots + g_k\bar{e_k} = 0$. This concludes the proof that $M/Rx$ is torsion-free.

Since $M/Rx$ can be generated by $<n$ elements and is torsion free, it follows by induction that $M/Rx$ is free.

Let $\bar{b_1},\ldots,\bar{b_q}$ be a basis for $M/Rx$.

Claim: The set $\{ x,b_1,\ldots,b_q \}$ is a basis for $M$, and thus $M$ is free.

It is easy to see that the given set spans $M$. Now, suppose that for some $s,s_1,\ldots,s_q \in R$ we have: $$sx+s_1b_1+\cdots+s_qb_q = 0$$ Then, applying the natural projection to $M/Rx$ gives: $$s_1\bar{b_1}+\cdots+s_q\bar{b_q}=0$$ Thus, since the $\bar{b_i}$ form a basis for $M/Rx$, it follows that $s_1=\cdots=s_q=0$. Thus $sx=0$. Since $M$ is torsion-free, it follows that $s=0$. Thus the given set is linearly independent, and is thus a basis for $M$.

For those of you that have followed the entire proof, thank you for lending your time. Does the proof hold?