Solution 2 works much faster.