modified various parts of the script and killed bugs