updated version - code loops through the result