Fix typo, and refactor out a large chunk of the code duplication.