updated the sad.R and mad.R functions