NEEDS_REBASE: Request to merge Mozilla's changes upstream

We're currently running this code on ~1300 servers. To get there we had to fix some bugs and make other improvements. All of our work is contained in this pull request. We hope that other people find this code helpful.