Cleanup and Error fixes

Add several frees that were missing to prevent memory leaks
Replace strcpy with strdup where appropriate
Replace _stat with __stat64 for building on Windows