Cherrypicked elements from https://github.com/Automattic/_s