Abstract out a bunch of common code.