refactor ifelse functions