Refactor duplicate code into a base class.