i've seen this code snippet also several times by now... when splitting this big function into smaller ones, i think this is something you can also factor out