How to derive the compressibility factor for a van der Waals gas?

So as far as I know, 
$$Z=\frac{p\_\mathrm{r}V\_\mathrm{r}}{nRT},$$ 
where $\mathrm{r}$ denotes the subscript for real.


Solving it for a van der Waals gas gave me the following result,
$$Z=1+\frac{p\_\mathrm{r} b}{RT}-\frac{an}{VRT}+\frac{an^2b}{V^2RT},$$
where $V$ is the volume of the container.


This doesn't quite tally with the form I see in the textbooks.
Where am I going wrong?


