moved loop invarients out to some inititalizing code