It looks good assuming your IF lines are looking for the right data and that your set lines are correct.
Make sure you have the htmlfree alias or it won't work.
Also, your last alias you have there isn't complete. It doesn't have a closing }, but it also seems like it's missing code for what to do if the variable isn't $null.
And don't forget to unset variables once they aren't needed anymore.
If the script isn't working still, mention what is working and what isn't working (I'm at work, so can't test it right now).