In calculating the gravitational potential energy of a system of two masses $m, M$, it is often assumed that $M\gg m$ in order to neglect motion of the larger mass $M$. As a result, the potential energy can be calculated to be $$U(r)=-W_g=-\int_{\infty}^{r}-\frac{GMm}{r^2}=-\frac{GMm}{r},$$ where we set $U(\infty)=0$.
In the case that $M$ and $m$ are of similar masses, as mass $m$ is brought from infinity to a distance $r$ (from a fixed origin, say the initial location of mass $M$), the gravitational force from $m$ on $M$ causes $M$ to move from its initial location, altering the gravitational force, causing the above derivation to be invalid. (The gravitational force from $M$ on $m$ is no longer simply $F(r)=-GMm/r$.) How do we find the potential energy of this system? Why can we take the larger mass to be fixed? Would we just consider the change in the position vector from $M$ to $m$?
Similarly, how would one compute the potential energy of a system of two (or more) charges, when moving a charge in from infinity alters the configuration that has been constructed? Would we have to "hold" the previous charges in place?
It seems like my understanding of the definition of potential energy is flawed. Can someone explain the definition which involves doing work to put together a system?