This 8 lines of code are used twice in the file. How about extracting them to a separate function?