version with loops