Refactor for cleanliness and add a bit of joint test code