improve code for checkDuplicatedInstances