Revised Script

Completely revised the script with functions, parameters and cleaned up
if statements.