I agree that it would look better to move it into a single function. I have added a commit to this branch. Since most affected code was already there before my changes, I would even keep it a separate commit.