added const where needed; use copied set for second iteration