Add missing cleanup upon error exit of main()
Use NULL instead of 0 for pointers