Clean up by extracting commonly used lines as a function.