Fixed issues with duplicate id collection. Code cleanup (or made it worse)