* Iterration through the files is done once now.
* Compacted the error handling.
* Splitted repeated code into functions.