Fix memory leak, unitialized vars and typos