replace hash map with optimized data structure to avoid excessice allocations
fix some bugs
Some cleanup
Move the palette expansion into the buffer.