Second version with loops