This is something I cherry-picked from upstream :)