Fixes #6: Allocates memory only if list != NULL