Moved temp variables out of loop