Copying functionality from our PHP Odilo library.