Merged relevant bits from the master branch.