minimized the for loops in generateUnique()