Backporting changes by @infograf768 from 3.x