Patch adapted from upstream b452a984b0fc522c21bb8df7d320bf13960974d0, which did not apply due to whitespace changes.