simplify using an inner loop