Modified to faster the for loops.