fix a whole bunch of bugs in this scripts