Remove n^2 algorithm and use map()