adding error checking after all malloc calls