Refactoring script

Refactoring script to decrease line length and make comments clearer.