This trick can and probably should be used in many other places. (cherry picked from commit 1032f8e1)