factor out boilerplate into a new partial.
Add tests. I believe this fixes #572, fixes #568, fixes #573.