Hoist some declarations out of the main loop.  Make sure vars are local.