Apply and adapt patch for #285, thanks to @eltjpm