Moved most of encoding-related stuff to a separate class.