Updated to include fred's substition function