Thanks bmispelon, kezabelle, jorgecarleitao, and timgraham for reviews. Backport of cd20b5d186 from master