put in outer loop code
rewrite appears to be working