Fix EOF handling; fix #656