This is a little bit neater IMO and does the same job. CCBUG: 249180 (cherry picked from commit 1749da9d)