fix ifelse bug and add tests