optimization of hasNext method