Multiple vulnerability fixes

Fixed multiple vulnerabilities, although there seem to be quite a few others.  I suggest auditing this application further, and regularly run it through static analysis.