Fix if index and add duplicate check/counter