implement the code from #4488 by @k-akarsh.

Closes #5286