Refactor using a loop to output array items