Hm, the logic in the function still seems to be too complicated, there's checks for EOL in three different places, and f(r) is checked twice. We should be able to do it with just one check of each.