Clean up by extracting the commonly used lines as a function.