Brett security audit 2014 Feb

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