Refactor the code a little bit to reduce duplicated code.

Reviewed by:	mjg
MFC after:	2 weeks