Refactored PBS generation code addressing @mgermain's comments.