refactoring: collapse repeating blocks into a loop

Change-Id: I50663283058e4eed5c12fe4af86e003c7a9ad525