refactor main file loop: added some javadoc