Could separate this code segment into a dedicated function?