Suppose I have some simple isolated system, so the entropy is given (according to the Wikipedia page on Hemholtz free energy) by $$ S = k\log Z +\frac{U}{T}+c$$ where $Z$ is the partition function, $U$ is average internal energy, $T$ is temperature, and $c$ is a constant. Some process adds a bit of heat to this system. Varying this equation gives: $$\delta S = k\delta (\log Z) + \frac{1}{T}\delta U - \frac{U}{T^2}\delta T$$ If the allowed energy levels are $E_i$, then $Z = \sum_i e^{-\frac{E_i}{kT}}$ and $U = \langle E_i\rangle=\frac{1}{Z}\sum_i E_i e^{- \frac{E_i}{kT}}$. So: $$\begin{align} \delta (\log Z) =& \frac{1}{Z}\delta Z\\ = & \frac{1}{Z}\sum_i e^{-\frac{E_i}{kT}}\left(-\frac{1}{kT}\delta E_i + \frac{E_i}{kT^2}\delta T \right)\\ =& -\frac{1}{kT}\langle \delta E_i\rangle + \frac{1}{kT^2}\langle E_i\rangle\delta T \end{align} $$ and this gives $$\delta S = -\frac{1}{T}\langle \delta E_i\rangle+\frac{1}{T}\delta \langle E_i\rangle$$ or $$ \delta \langle E_i\rangle - \langle \delta E_i\rangle = T\delta S$$ By the second law of thermodynamis, the right hand side should equal $\delta Q$. But on the left, I can also consider the the change in average energy by considering it just as a probabilistic average: $$\begin{align} \delta\langle E_i\rangle = & \delta\left(\sum_i E_i p_i\right)\\ = & \sum_i \delta E_i p_i + \sum_i E_i\delta p_i\\ = & \langle \delta E_i\rangle + \sum_i E_i\delta p_i \end{align}$$
Putting this together gives: $$ \delta Q = T\delta S = \sum_iE_i\delta p_i$$
This almost makes sense to me: The amount of heat put into the system should be the sum, over different energy levels, of the change in the number of particles at that energy level. What this shows is that that the heat put in is the sum over different energy levels of the change in the percentage of particles at that energy level.
The problem here is that I think the heat input should be measured in units of energy, but here this seems be be energy/particle. That is, if I put in 1000 joules of heat into a system at temperature $T$, the entropy increase should be the same whether there were $10^{23}$ or $10^{30}$ particles. But what I derived seems to imply that the entropy increase will be $10^7$ times less in the $10^{30}$ particle system, because the change in energy proportions will be smaller.
Is there something wrong with my derivation, or with my understanding of heat and entropy?