Implement entropy decoder and fix bug in encoder