Revised implementation for all children of a term. Added function for calculating the arity of a term.
Simplified substitutions.