Limit scope where applicable.

I am the farthest thing from a C expert, just tinkering. Was hoping this would help with a memory leak, no such luck :(