Fixed bugs in script