prematurely optimized to avoid unnecessary iterators hit. code is ugly and need to be polished more