modified to make each element processed just once.