Refactor chunking code for clarity
Fix XSLT 2.0 compatibility issues