added a hasNext() check to the next() method