Optimized encoder for packet error correction and fixed some bugs