Replace function added (and minor changes)