rewrite of some loops