Replaced class(fit)=="name" with inherits(x=fit, what="name") in places, which should be safer.
class(fit)=="name"
inherits(x=fit, what="name")