This is a refactor to prepare the next commit. It doesn't do much on its own, but is separated for ease of review.