Improve error logging.
Fix memory free bug.