Abstract some boilerplate out into its own function.

I was reading the code, came across this, and could not resist the urge to reduce the copy/paste.