Takes a long time for N > 300,000