fixed a script error in some cases (variable defined functions need to be declared before they are used