refactoring code for EOF handling