added stopifnot to make sure x is numeric