New script version, fixed several bugs