Rewrite entire script

we rewrite the entire script to get rid of errors.