Duplicated logic of checking EOL. Maybe move it into a helper function?