Change the inner loop to make it faster