Generalize pairwise layout code. Fixes #24.