Fix 2 possible buffer overflows and a reference to an automatic variable
outside of it's scope. I don't even know why it worked for so long...