LZW Compression adapted from RosettaCode