added core logic to handle substitution - won't work for sure