Added checks and exits for malloc and asprintf errors