Fix unexpected EOF bug