Sane encoding handling, plug memory leaks.