Minor optimizations.

Move strlen out of for loop.
Check for empty string without calling strlen.
Remove redundant null checks before free.