Simplified code with BufferedIterator