Refactor doIfOrElse, backport #1137
Minor clean up