Fixed bug #22800 and removed a redundant macro

Also ports 31d459 and 1b7fc8 from master.