simplify a couple of loops