Add comments etc, in sync with Python version
Debug logging
Remove debug logging
Tweaks after writing PHP version