Better writing of a loop