Improve looping at end of files Fixes #27