changed the algorithm so that tests with N == 30 would finish.