Cleaned up code, add sorting to ensure reliable huffman coding