Also tidied up a few lines, got rid of apply(), added a comment.
Attach a file by drag & drop or click to upload