modified substitute, checked corner cases