Refactoring more code.
Completed refactoring of script.