Add some DRYness to this snippet and fix the tests as needed afterwards.