Fixed bug in ifelse stmt