refactor main file loop: more javadoc