Audit 2014 02

This pull request fixes XSS vulnerabilities and ways to crash the script. See individual commit messages for more details.