Almost ready to merge back to mainline after cherrypicking some changes.