Reorganized and rewrote the scripting