# Varying a function

Hi i am trying to vary $\int a^{3}(t)(0.5\dot{\phi}^{2}-\frac{1}{2a^2}(\nabla \phi)^{2} -V) d^3 x$
I understand that one varyies w.r.t phi so it becomes:
$\int a^{3}(t)(\dot{\phi}\delta \dot{\phi}-\frac{1}{a^2}(\nabla \phi)(\delta \nabla \phi) -V'\delta \phi) d^3 x$

I can't see why it would then becomes $\int (-\frac{d}{dt}(a^{3}\dot{\phi})+a(\nabla^{2} \phi) -a^3V') d^3 x$

I.e where do the variations go why does it become $\partial_{\mu}$ that then moves before the terms not after them , i realise that the metric used is (-,+++)