Refactor the function run_pelog().

run_pelog() was just under 1000 lines long, and now its been brought
down to less than 300 lines. It can still be fixed up a bit, but this is
a good first step of improvements.