updated replacement logic