Refactor the code a little bit to reduce duplicated code.

Reviewed by:	mjg
MFC after:	2 weeks


git-svn-id: svn+ssh://svn.freebsd.org/base/head@272154 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f