Move codes out of loop code