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