added EOF detection