Tidied up, not adding extra derivatives w.r.t. parameters yet.