Replaced assertions with error checking code malloc