Type fixes, C99 adherence, additional free() calls and more memory leak fixes

Hey sorry this is all in one PR but I got a little behind on sending patches back.

I've been running all these changes on a couple systems as of their commit date (and some before that) let me know if you see any problems with these or have any questions. Also please do not hesitate to take some commits or reject others.