Adds comments, slight refactor clarifications to make the code
easier to follow.
This is a partial backport of [[https://github.com/bitcoin/bitcoin/pull/17487 | core#17487]]
https://github.com/bitcoin/bitcoin/pull/17487/commits/6d8affca96c7a34f5f104c5a3122e7420ffc083c
Depends on D16156