Thanks to Rodolfo Carvalho and Piotr Kasprzyk for the patch. Partial backport of ea4da8e6 from master.