Inspired by this Stack Overflow question.