Rearrange a lot

partly for sanity, partly to facilitate passing many pairs around