Prove that if $\displaystyle (ab)^{2}=a^{2}b^{2}$ in a group G, then $\displaystyle ab=ba$.

My proof so far:

Now (ab)^{2}=a^{2}b^{2} for each a,b \in G,

so $\displaystyle a^{2}b^{2}=a^{2}b^{2}$

implies $\displaystyle a^{2}b^{2}=b^{2}a^{2}$

then, there exist a^{-1},b^{-1} in G

(a^-1)(a^2b^2)(b^-1) = (a^-1)(b^2a^2)(b^-1)

ab=ba.

Am I allow to switch the a and b like that?