fixed atEnd() function