Backport a few useful changes from sunglasses branch.