Reorganized the script: Moved the main loop into a function which returns the used values.