This is now hopefully more straightforward than it was, and should allow future refactoring as needed.