Here is a working copy of the final script.