Fix handling special cases in "substitute", portability fixes

Rebased #4438 against staging. cc @wmertens, @bjornfor.