Optimized memory allocation and fixed main to return zero upon completion