perfect the firstSet algorithm