Extract this code block into an own function.

This code block is sufficiently self-contained.

Change-Id: I0eb2d3ee8175af60394ff4c408a9c245f77044d8