Here’s some improvements:
https://github.com/discourse/discourse/commit/0e710dc573a02cdfb05f8c88d3024aacbbb81872