the if statement can go just around the atEnd = ... line, and no need for an else.