Loop instead of duplicate code
Merge pull request #1484 from tigrang/code-cleanup

Loop instead of duplicate code