Fixing script and converting to javascript