Made minor modification to main for loop so that it runs in order N
time.