A more or less working version of the script.