Switch back to using ifelse() for performance improvement