Bugfix: fixed bug in loop termination when reading to end