Fixed wrong free function in history processing.

Also moved freeing to the caller.