This should be `x` instead of `val`. Also, I'd suggest not using `ifelse` unless you're using it specifically to handle for vector input; it's actually slower than `if ... else`, and more importantly, it's a signal to others that the input can be vectors.