Don't crash on end of stream

Fixes #1.