Add RLE encoding and bug fix