fixed bug and added error handling for calloc