Manually included the three changes (with code corrections) from pull request #1257.