I think this function has a bug if there are multiple chains in the topology. Then the residue indices won't be unique.