Martingale representation theorem Trying to figure out how to solve problems on the 'form':
Find a real number $z$ and a square integrable, adapted process $\psi(s,w)$ such that
$$G(w) = z + \int \psi(s,w)\,dB_s(w)$$
for som process $G(w)$. 
In the case I'm working on now I have $G(w) = (B^2_T(w)-T)\exp(B_T(w)-T)$.
So using the Martingale representation theorem I have that:
$$G(w) = E[G] + \int \psi(s,w)\,dB_s(w)$$
and I've already calculated $E[G]$ to be $T^2e^{-T/2}$. So it only remains to show what $\psi(s,w)$ is.
What I've done now is to apply the Itô formula on $G$, as he's done in other old exams, but I can't really understand what he's doing because his handwriting is terrible. But as I said he uses the Itô formula and uses the '$dB_s$'-term as the $\psi(s,w)$ but he's changing it and that step I can't really tell what he is doing. Does anyone know?
From the Itô formula I get $dG(w) = (B_s^2 + 2B_s - 2s)e^{B_s-s}dB_s(w) + (\ldots)dt$
Thanks in advance!
 A: First note that $G=X_TY_T\mathrm e^{-T/2}$ where the processes $X$ and $Y$ are defined for every $t\geqslant0$ by
$$X_t=B_t^2-t,\qquad Y_t=\mathrm e^{B_t-t/2}.
$$
The identity $M^T_t=E[X_TY_T\mid\mathcal F_t]$ defines a martingale $(M^T_t)_{0\leqslant t\leqslant T}$ such that $M^T_T=X_TY_T$ and $M^T_0=E[X_TY_T]$. Thus, $\mathrm dM^T_t=K^T_t\mathrm dB_t$ for some process $(K^T_t)_{0\leqslant t\leqslant T}$, and
$$G=\mathrm e^{-T/2}M^T_0+\mathrm e^{-T/2}\int_0^TK^T_t\mathrm dB_t.
$$ 
To sum up, this warm up paragraph shows that it suffices to identify $M_0^T$ and the process $K^T$.
You already know that $M_0^T=T^2\mathrm e^{-T/2}$.
To identify $K^T$, fix some $t\lt T$, define $u=T-t$, and consider the processes $\bar B$, $\bar X$ and $\bar Y$ defined for every $s\geqslant0$ by
$$\bar B_s=B_{t+s}-B_t,\qquad\bar X_s=\bar B_s^2-s,\qquad\bar Y_s=\mathrm e^{\bar B_s-s/2}.
$$
Then,
$$
X_TY_T=((B_t+\bar B_u)^2-t-u)Y_t\bar Y_u=X_tY_t\bar Y_u+2B_tY_t\bar B_u\bar Y_u+Y_t\bar B_u^2\bar Y_u.
$$
Since $\bar B$ is independent of $\mathcal F_t$ and $(\bar B,\bar Y)$ is distributed like $(B,Y)$, this yields
$$
M_t^T=E[X_TY_T\mid\mathcal F_t]=A_0(T-t)X_tY_t+2A_1(T-t)B_tY_t+A_2(T-t)Y_t,
$$
where, for every integer $k\geqslant0$,
$$
A_k(u)=E[B_u^kY_u].
$$
Since one already knows that $M^T$ is a martingale, one is only interested in the martingale part of the RHS of the last identity above giving $M_t^T$.
Note that $\mathrm dX=2B\mathrm dB$ and $\mathrm dY=Y\mathrm dB$, hence the martingale part of $\mathrm d(BY)$ is $Y(B+1)\mathrm dB$ and the martingale part of $\mathrm d(XY)$ is $Y(X+2B)\mathrm dB$.
To compute the functions $A_k$ for $0\leqslant k\leqslant2$, note that, for every $x$, the identity
$$
Y^x_u=\exp((1+x)B_u-(1+x)^2u/2)=Y_u\exp(xB_u-xu-x^2u/2),
$$
defines a martingale $Y^x$ such that $Y^x_0=1$ hence $E[Y^x_u]=1$ for every $u\geqslant0$. Expanding this in powers of $x$ yields
$$
E[Y_u]=1,\quad E[(B_u-u)Y_u]=0,\quad E[((B_u-u)^2-u)Y_u]=0,
$$
that is,
$$
A_0(u)=1,\quad A_1(u)=uA_0(u)=u,\quad A_2(u)=2uA_1(u)-(u^2-u)A_0(u)=u^2+u.
$$
Finally, for every $0\leqslant t\leqslant T$,
$$
K_t^T=(B_t^2-t+2(T-t+1)B_t+(T-t)^2+3(T-t))\cdot\mathrm e^{B_t-t/2}.
$$
A: As far as I can see it's wrong what he is doing there. The claim is that
$$2 \int_0^T B_t \cdot \exp \left( B_t-\frac{t}{2} \right) \, dt = T^2$$
But this can't be true. Since $t \mapsto B_t(w) \cdot \exp \left( B_t-\frac{t}{2} \right)(w)$ is continuous almost surely we can apply the fundamental theorem of calculus and obtain
$$2 B_T \cdot \exp \left(B_T- \frac{T}{2} \right) = 2 T \quad \text{a.s.}$$
which would imply
$$B_T = T \cdot \exp \left(\frac{T}{2}-B_T\right) \geq 0$$
... and this is not correct.

I applied Itô's formula to $f(t,x) := (x^2-t) \cdot \exp (x-t)$ and obtained
$$\underbrace{f(t,B_t)}_{G_t}-\underbrace{f(0,0)}_{0}= \int_0^t \exp(B_s-s) \cdot (B_s^2+2B_s-s) \, dB_s \\ + \frac{1}{2} \int_0^t \exp(B_s-s) \cdot \left(-\frac{1}{2} B_s^2+2B_s + \frac{s}{2} \right) \, ds$$
