Modified according to mtoman's comments.