optimize the replacement