Rewrite loop code to be non-static