Copying from the 1.2 version of the script