Code for figure 1, to run overnight