Cleans up reundant code and reduces the diff of the next commit.
This is a backport of core#21062 [1/4]
https://github.com/bitcoin/bitcoin/pull/21062/commits/9db10a55061e09021ff8ea1d6637d99f7959035f