solved looping problem with local variables