Add replace_instance function