Fixed early buffer free.
Fixed file name generation call.
Cleaned up error handling a little.