replaced Stream with While