This is a partial backport of [[https://github.com/bitcoin/bitcoin/pull/27596 | core#27596]] and [[https://github.com/bitcoin/bitcoin/pull/28562 | core#28562]]
https://github.com/bitcoin/bitcoin/pull/27596/commits/99839bbfa7110c7abf22e587ae2f72c9c57d3c85
https://github.com/bitcoin/bitcoin/pull/28562/commits/a482f86779a6182d87004b463c0eaf21038181c3