Port changes for #230 from master to 1.7.