Visual Studio Compatibility Fix

Moved variable declarations to the top of functions or loops to maintain
VS compatibility as VS doesn't support C99 standard/