Adding Dan's improvement, and making a bit of change to it