Removing unnecessary nested loop structure and unused variables from paletteMap.