Refactor code to make it more readable. No functional change.
Fix string escaping which was previously not done correctly.
Switch from hex to base64 for blob