Remove n^2 loop