fixed off by one string truncation issue
moved duplicated code to a method
fixed bug causing vulnerabilities to be missed