Re-factor pipe code into separate utility library / class.

Ensure batch processing work correctly.