fix unitialized variables (found using valgrind)
take a part of the loop interator indepandant out