Shortened the script, fixed some bugs