Where did I go wrong in my evaluation of the integral of cosine squared? $$\int{\cos^2(x)}dx$$
Where did I go wrong in my evaluation of this integral?
$$=x\cos^2x - \int-2x\sin(x)\cos(x)\,dx$$
$$=x\cos^2x + \int x\sin(2x)\,dx$$
$$=x\cos^2x + \left(\frac {-x\cos(2x)}2 -\int \frac{-\cos(2x)}2\,dx\right)$$
$$=x\cos^2x + \left(\frac {-x\cos(2x)}2 + \frac 12\cdot\frac{\sin(2x)}2\right)$$
$$x\cos^2x-\frac{x\cos(2x)}2+\frac{\sin(2x)}4 + C$$
And this is clearly wrong, but I don't know where I messed up in my calculations. Would anyone mind correcting me somewhere?

Notice,Let  $$I=\int \cos^2 xdx$$
$$\implies I=\int \cos x\cos xdx$$ $$=\cos x\int \cos x-\int (-\sin x)\sin xdx$$ $$=\cos x(\sin x)+\int \sin^2 xdx$$ $$=\frac{1}{2}(2\sin x \cos x)+\int (1-\cos^2 x)dx$$ $$=\frac{1}{2}\sin 2x+\int dx-\int \cos^2 xdx$$
$$I=\frac{1}{2}\sin 2x+x-I+c$$
$$2I=\frac{1}{2}\sin 2x+x+c$$
$$I=\frac{1}{4}\sin 2x+\frac{x}{2}+C$$
