Major improvements to the script