Fix/eof handling

Supersedes and closes #163.