Updated freeing functions.

Freeing functions have been updated. The function checks if the input
pointer is NULL and returns if so. Otherwise, it performs the
appropriate freeing.