Revised substitution module.

Fixed composition of substitutions. Some refactoring in the apply
method.