Changed to calloc and added check that mem allocation was successful.