I am trying to derive the Hamilton-Jacobi equation directly. I almost get the correct answer; however, I keep running into an issue with the sign on the $p\dot{q}$ term, where $p$ is the canonical momentum and $\dot{q}$ is the time derivative of the generalized coordinate $q$. Below is the path that I have been taking to deriving it.
Take \begin{equation} S(q_i,t_i,q_f,t_f) = \int_{t_i}^{t_f}\mathrm{d}t \ L(q,\dot{q})\tag{i} \end{equation} to be the classical action evaluated along the classical path from $(q_i,t_i)\rightarrow (q_f,t_f)$. The statement of the Hamilton-Jacobi equation is $$\begin{align} \frac{\partial S(q_i,t_i,q_f,t_f)}{\partial t_i}=H_i \mathrm{ \ \ and \ \ } \frac{\partial S(q_i,t_i,q_f,t_f)}{\partial t_f}=-H_f. \end{align}\tag{ii}$$ To derive the first of these two relations (the latter follows directly from the first, of course), take the time derivative directly: \begin{align} \frac{\partial S(q_i,t_i,q_f,t_f)}{\partial t_i} \underbrace{=}_{(1)} \frac{\partial}{\partial t_i}\int_{t_i}^{t_f}\mathrm{d}t \ L(q,\dot{q}) \\ \underbrace{=}_{(2)} -L(q_i,\dot{q}_i) + \int_{t_i}^{t_f}\mathrm{d}t \ \frac{\partial L(q,\dot{q})}{\partial t_i} \\ \underbrace{=}_{(3)} -L(q_i,\dot{q}_i) + \int_{t_i}^{t_f}\mathrm{d}t \ \bigg[\frac{\delta L(q,\dot{q})}{\delta q(t)}\frac{\partial q(t)}{\partial t_i}+\frac{\delta L(q,\dot{q})}{\delta \dot{q}(t)}\frac{\partial \dot{q}(t)}{\partial t_i}\bigg] \\ \underbrace{=}_{(4)} -L(q_i,\dot{q}_i) + \int_{t_i}^{t_f}\mathrm{d}t \ \bigg[\underbrace{\frac{\delta L(q,\dot{q})}{\delta q(t)}-\frac{\mathrm{d}}{\mathrm{d}t}\frac{\delta L(q,\dot{q})}{\delta \dot{q}(t)}}_{=0\mathrm{ \ by \ eqn. \ of \ motion}}\bigg]\frac{\partial q(t)}{\partial t_i}+\bigg[\frac{\delta L(q,\dot{q})}{\delta \dot{q}(t)}\frac{\partial q(t)}{\partial t_i}\bigg]_{t_{i}}^{t_{f}} \\ \underbrace{=}_{(5)} -L(q_i,\dot{q}_i) - p_i \dot{q}_i \end{align} The second equality follows from the Leibniz integration rule, third from the chain rule, fourth from integration by parts, and the fifth from \begin{equation} \frac{\partial L}{\partial \dot{q}} = p. \end{equation} This contradicts the statement of the Hamilton-Jacobi equation, as \begin{equation} H = p\dot{q}-L. \end{equation} I feel like I am going crazy. What am I missing on the sign here?