Further generalize commit 76082c30b4ae04.