Added check for end of list