Prevent infinite loop on early end of input (fixes #3)