Thanks Erik Romijn for the suggestion. Backport of 3776926cfe503f16c7195621da20c5b89bda70a2 from master