#3 refactoring and closing input streams