Moved the custom patch apply function to the utils module.
Refactored document and protocol classes to make text changes less complicated.